package models

import (
	"fmt"
	"github.com/astaxie/beego/orm"
	"h1/utils"
	"time"
)

type Project struct {
	Id          int64     `orm:"pk;auto"`   // 项目ID
	Title       string    `orm:"size(255)"` // 项目标题
	Describe    string    `orm:"size(255)"` //项目描述
	Background  string    `orm:"size(255)"` //项目背景
	Target      string    `orm:"size(255)"` //项目目标
	ParentId    int64     `orm:"size(11)"`  // 项目父级ID
	Status      int64     `orm:"size(1)"`   //0:正常
	CheckStatus int64     `orm:"size(1)"`   //0：草稿箱，1：审核中；2：审核通过；3：审核失败
	CreateAt    time.Time `orm:"auto_now_add;type(datetime)"form:"-"`
	CreateBy    int64     `orm:"size(11)"` //用户名称
	UpdateAt    time.Time `orm:"auto_now;type(datetime)"`
	UpdateName  string    `orm:"size(20)"`  //
	CreateName  string    `orm:"size(20)"`  //更新者姓名
	UpdateBy    int64     `orm:"size(11)"`  //更新者姓名
	Mark        string    `orm:"size(255)"` //备注
	EditBy      string    `orm:"size(255)"`
	EditFlag    int64     `orm:"size(255)"` // 0:查看权限；1编辑权限
	SubmitLog   string    `orm:"size(255)"`
	CheckLog    string    `orm:"size(255)"`
}

// 获取模型名称
// 获取模型表名
func (m *Project) TableName() string {
	return "project"
}

// 获取全部用户
func (m *Project) GetAllUser() []*Project {
	info := Project{}
	list := make([]*Project, 0)
	_, _ = info.Query().All(&list)
	return list
}

// 查询方法
func (m *Project) Query() orm.QuerySeter {
	return orm.NewOrm().QueryTable(m)
}

// 添加用户
func (m *Project) Insert() error {
	_, err := orm.NewOrm().Insert(m)
	return err
}

// 删除用户
func (m *Project) Delete() error {
	_, err := orm.NewOrm().Delete(m)
	return err
}

// 修改用户信息
func (m *Project) Update(fields ...string) error {
	_, err := orm.NewOrm().Update(m, fields...)
	return err
}
func (m *Project) Read(fields ...string) (int64, error) {
	if err := orm.NewOrm().Read(m, fields...); err != nil {
		return 0, err
	}
	return m.Id, nil
}

// 通过ID查询单个用户信息
func (m *Project) GetProjectById(uid int64) (*Project, error) {
	info := &Project{}
	err := info.Query().Filter("id", uid).One(info)
	return info, err
}

// 通过父节点获取project
func (m *Project) GetProjectListByParentId(uid int64) []*Project {
	info := &Project{}
	list := make([]*Project, 0)
	_, _ = info.Query().Filter("check_status", 2).Filter("parent_id", uid).All(&list)
	return list
}

// 通过父节点获取project
func (m *Project) GetProjectListByGTParentId(uid int64) []*Project {
	info := &Project{}
	list := make([]*Project, 0)
	_, _ = info.Query().Filter("check_status", 2).Filter("parent_id__gt", uid).All(&list)
	return list
}

// 获取用户总数
func (m *Project) GetProjectCounts() int64 {
	qs := orm.NewOrm().QueryTable(m)
	count, _ := qs.Filter("check_status", 2).Count()
	return count
}

// 获取每天的用户数
func (m *Project) GetProjectPerDayCounts(string2 string) int64 {
	strMin := string2 + " 00:00:00"
	strMax := string2 + " 23:59:59"
	qs := orm.NewOrm().QueryTable(m)
	cnt, _ := qs.Filter("check_status", 2).Filter("create_at__gte", strMin).Filter("create_at__lte", strMax).Count()
	return cnt
}

// 获取每天区间的用户数
func (m *Project) GetProjectRandDayCounts(old string, new string) int64 {
	strMin := old + " 00:00:00"
	strMax := new + " 23:59:59"
	fmt.Println("-----------------", strMin, strMax)
	qs := orm.NewOrm().QueryTable(m)
	cnt, _ := qs.Filter("check_status", 2).Filter("create_at__gte", strMin).Filter("create_at__lte", strMax).Count()
	return cnt
}
func GetAllProjectLst(uid int64) map[string]interface{} {

	resDic := utils.MakeJson()
	//我创建的
	sqls_1 := "SELECT  *  from tb_project WHERE 1 > 0 and check_status != 0 and create_by = " + utils.Int64ToString(uid)
	maps_1, _ := MutiJoinTableQuery(sqls_1)
	resDic["myCreateArr"] = productUserList(maps_1)
	////我修改的
	//sqls_2 := "SELECT  *  from tb_scene WHERE 1 > 0 and check_status != 0 and update_by = " + utils.Int64ToString(uid) + ""
	//maps_2, _:= MutiJoinTableQuery(sqls_2)
	//resDic["myEeditArr"] = productUserList(maps_2)
	// 允许我修改的
	sqls_3 := "SELECT  *  from tb_project WHERE 1 > 0  and create_by !=" + utils.Int64ToString(uid) + " and edit_by  like '%" + utils.Int64ToString(uid) + "%'"
	maps_3, _ := MutiJoinTableQuery(sqls_3)
	resDic["editByMeArr"] = productUserList(maps_3)
	// 所有的
	sqls_4 := "SELECT  *  from tb_project WHERE 1 > 0 "
	maps_4, _ := MutiJoinTableQuery(sqls_4)
	resDic["allArr"] = productUserList(maps_4)
	// 我保存的
	sqls_5 := "SELECT  *  from tb_project WHERE 1 > 0 and create_by =" + utils.Int64ToString(uid) + " and check_status = 0"
	maps_5, _ := MutiJoinTableQuery(sqls_5)
	resDic["mySaveArr"] = productUserList(maps_5)
	return resDic
}
